<?php
require_once ('db.php');

$where = '';

if (isset($_GET['status'])) {

  $status = $_GET['status'];
  $id = $_GET['id'];
  $isUpdated = updateStatus($status,$id );
  if($isUpdated == 1){
    echo "status updated successfully";
  }
}

if(isset($_POST['search'])){
  //pr($_POST);
  $status = $_POST['status'];
  $search = trim($_POST['search']);
  $where = " WHERE ( `uname` LIKE '%$search%' OR `email` LIKE '%$search%') AND `status`='$status'";
}


if (isset($_POST['email']) && !empty($_POST['email'])) {
  unset($_POST['cpassword']);
  unset($_POST['submitUsers']);
  //pr($_POST);
  $q = '';
  foreach ($_POST as $colName => $colValue) {
    pr("`$colName`='$colValue', ");
    $q .= "`$colName`='$colValue', ";
  }
  $q .= " `added_date`=NOW(),`status`='1'";
  $insertQuery = " INSERT INTO `users` SET  $q";
  mysql_query($insertQuery) or die(mysql_error());
  if (mysql_affected_rows() > 0) {
    echo "<h2>USer Inserted successfully</h2>";
  }
}

## GET TOTAL NUMBER OF USERS##
///$selCountQuery = "SELECT COUNT(*)  FROM `users`";
$selCountQuery = "SELECT COUNT(*) as `total` FROM `users` $where";
$selCountResult = mysql_query($selCountQuery) or die(mysql_error());
$selCountAns = mysql_fetch_assoc($selCountResult);
$totalUsers = $selCountAns['total'];
######## PAGING ############
$pageNum = isset($_GET['page']) ? $_GET['page'] : 1;
$recordPerPage = 10;
$limit = ($pageNum - 1) * $recordPerPage;
$totalPages = ceil($totalUsers / $recordPerPage);
###Select Users from Database####3
$selQuery = "SELECT * FROM `users` $where LIMIT $limit,$recordPerPage";
pr($selQuery);
$selResult = mysql_query($selQuery) or die(mysql_error());
##############################
//pr($_SERVER);
?>

<form name="users" method="post" action="">
  <table border="1" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">Please fill the details</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Name :</td>
        <td>
        <input type="text" required name="uname">
        </td>
      </tr>
      <tr>
        <td>Email :</td>
        <td>
        <input type="email" required name="email">
        </td>
      </tr>
      <tr>
        <td>Password :</td><td>
        <input type="password" autocomplete="off" required name="password">
        </td>
      </tr>
      <tr>
        <td>Confirm Password :</td>
        <td>
        <input type="password" required name="cpassword">
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
        <input type="submit" name="submitUsers" value="I am done!!">
        </td>
      </tr>
    </tbody>
  </table>
</form>
<hr>
    <form action="" method="post">
<div>

  <span >Total Users :<strong><?=$totalUsers?></strong></span>
  <span>
    <label><input required type="radio" name="status" value="0" />Inactive</label>
    <label><input required type="radio" name="status" value="1" />Active</label>
  </span>
      <span style="float:right">
        <input type="text" required placeholder="search here" name="search" />
        <input type="submit" name="submit" value="Search"/>
      </span>

</div>
</form>

<hr>
<table border="1" cellpadding="5" cellspacing="0" align="center" width="80%">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
      <th>Email</th>
      <th>Added Date</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
    <?php
while($user = mysql_fetch_assoc($selResult))
{
      ?>
    <tr>
      <td><a href="edit_user.php?id=<?=$user['id']?>" target="_blank"><?=$user['id']?></a></td>
      <td><?=$user['uname']?></td>
      <td><a href="mailto:<?=$user['email']?>"> <?=$user['email']?></a></td>
      <td><?=date('d-M-Y g:i:s a',strtotime($user['added_date']))?></td>
      <td><?=getStatus($user['status'],$user['id'])?></td>
    </tr>
    <?php
}
    //end while
    ?>
  </tbody>
</table>
<!----Paging Numbers START --->
<table border="1" cellpadding="5" cellspacing="0" align="center" width="80%">
  <thead>
    <tr>
      <th><a href="select_user_search.php?page=<?=$pageNum-1?>">&lt;&lt;&lt;Prev</a></th>
<?php
for($i=1 ; $i<=$totalPages ; $i++) {

  if($i == $pageNum){
?>
      <th ><span style="color:red;font-size:30px;"><?=$i?></span></th>
<?php
  } else  {//end if
?>
      <th ><a href="select_user_search.php?page=<?=$i?>"><?=$i?></a></th>
<?php
  }//end else

}//end for
?>
      <th><a href="select_user_search.php?page=<?=$pageNum+1?>">Next&gt;&gt;&gt;</a></th>
  </thead>
</table>
